var admin_WebHosting = function() {

    var location4VMTable, servicename4VMTable;

    //$(document).ready(function() {
    $('#adminWebHostingLogDialog').dialog({
        width: 500,
        autoOpen: false,
        modal: true,
        title: 'System Log',
        buttons: {
            OK: function() {
                $(this).dialog("close");
            },
            Refresh: function() {
                $('#adminWebHostingLogTextArea').attr('src', '#');
                $('#adminWebHostingLogTextArea').attr('src', '/webhosting/execute_wait_cacheable/webapi.log2html.raw');
            }

        }
    });

    $('#adminWebHostingLogButton').click(function(event) {
        //$.post('/webhosting/execute_wait/webapi.log2html.json', {
        //        },
        //        function(serverResponse) {
        //                  var text=serverResponse;
        //      $('#adminWebHostingLogTextArea')[0].value=text;
        //        },
        //        'json');
        $('#adminWebHostingLogTextArea').attr('src', '/webhosting/execute_wait_cacheable/webapi.log2html.raw');
        $('#adminWebHostingLogDialog').dialog('open');
    });

    function adminWebHostingServiceInfoTable() {
        $('#adminWebHostingServiceTable').dataTable().fnClearTable();
        for (var j = 0; j < allsites.length; ++j) { (function(siteName) {
                $.post('/' + siteName + '/webhosting/execute_wait/webapi.serviceTableByAdmin.json', {},
                function(serverResponse) {
                    var servicename;
                    var servicetype;
                    var webftp;
                    var php;
                    var mysql;
                    var username;

                    for (var i = 0; i < serverResponse.length; i += 1) {
                        servicename = serverResponse[i].serviceName;
                        servicetype = serverResponse[i].serviceType;
                        webftp = serverResponse[i].webftp;
                        php = serverResponse[i].phpMyAdmin;
                        mysql = serverResponse[i].mysql;
                        username = serverResponse[i].userName;
                        $('#adminWebHostingServiceTable').dataTable().fnAddData([
                        siteName, servicename, servicetype, webftp, php, mysql, username]);
                        //console.log(serverResponse);
                        //console.log(serverResponse.length);
                        //  console.log(tnc );
                        //  console.log(inputfile );
                        //  console.log(command );
                        //  console.log(state );
                        //  console.log(result );
                    }
                },
                'json');
            })(allsites[j]);
        }

    };

    $('#adminWebHostingRefreshButton').click(function() {
        for (var j = 0; j < allsites.length; j += 1) { (function(siteName) {
                $.post('/' + siteName + '/webhosting/execute_wait/webapi.serviceTableByAdmin.json', {},
                function(serverResponse) {
                    var servicename;
                    var servicetype;
                    var webftp;
                    var php;
                    var mysql;
                    var username;

                    for (var j = $('#adminWebHostingServiceTable').dataTable().fnSettings().aoData.length - 1; j >= 0; j--) {
                        var name = $('#adminWebHostingServiceTable').dataTable().fnSettings().aoData[j]._aData[0];
                        if (name == siteName) {
                            var node = $('#adminWebHostingServiceTable').dataTable().fnGetNodes(j);
                            $('#adminWebHostingServiceTable').dataTable().fnDeleteRow(node);
                        }
                    }

                    for (var i = 0; i < serverResponse.length; i += 1) {
                        servicename = serverResponse[i].serviceName;
                        servicetype = serverResponse[i].serviceType;
                        webftp = serverResponse[i].webftp;
                        php = serverResponse[i].phpMyAdmin;
                        mysql = serverResponse[i].mysql;
                        username = serverResponse[i].userName;
                        $('#adminWebHostingServiceTable').dataTable().fnAddData([
                        siteName, servicename, servicetype, webftp, php, mysql, username]);
                       // console.log(serverResponse);
                       // console.log(serverResponse.length);
                        //  console.log(tnc );
                        //  console.log(inputfile );
                        //  console.log(command );
                        //  console.log(state );
                        //  console.log(result );
                    }
                },
                'json');
            })(allsites[j]);
        }

    });

    $('#adminWebHostingVMTableDialog').dialog({
        width: 450,
        autoOpen: false,
        modal: true,
        title: 'Dedicated VMs',
        buttons: {

            OK: function() {
                //var this_ = this;
                //window.setTimeout(function() {
                $(this).dialog("close");
                //    window.location.href = '/session/logout';
                //},
                //1000);
            },
            Refresh: function() {
                //var this_ = this;
                //window.setTimeout(function() {
                $.post('/' + location4VMTable + '/webhosting/execute_wait/webapi.queryLoad.json', {
                    serviceName: servicename4VMTable
                },
                function(serverResponse) {
                    var vmid;
                    var cpu;
                    var mem;
                    var averagecpu;
                    var averagemem;

                    $('#adminWebHostingVMTable').dataTable().fnClearTable();

                    for (var i = 0; i < serverResponse.verbose.length; i += 1) {
                        console.log(serverResponse);
                        console.log(serverResponse.verbose.length);
                        vmid = serverResponse.verbose[i].vmId;
                        cpu = serverResponse.verbose[i].cpu;
                        mem = serverResponse.verbose[i].mem;

                        $('#adminWebHostingVMTable').dataTable().fnAddData([
                        vmid, cpu, mem]);

                        //  console.log(tnc );
                        //  console.log(inputfile );
                        //  console.log(command );
                        //  console.log(state );
                        //  console.log(result );
                    }
                    averagecpu = serverResponse.average.cpu;
                    averagemem = serverResponse.average.mem;
                    $('#adminWebHostingVMTable').dataTable().fnAddData(['Average', averagecpu, averagemem]);
                },
                'json');
                //    window.location.href = '/session/logout';
                //},
                //1000);
            }
        }
    });

    $('#adminWebHostingLoadButton').click(function(event) {
        var anSelected = fnGetSelected(adminWebHostingServiceTable);
        var a = $('td', anSelected);
        if (a.length === 0) {
            alert("please select one row!");
            return 0;
        }

        location4VMTable = $(a[0]).text();
        servicename4VMTable = $(a[1]).text();

        $('#adminWebHostingVMTable').dataTable().fnClearTable();

        $.post('/' + location4VMTable + '/webhosting/execute_wait/webapi.queryLoad.json', {
            serviceName: servicename4VMTable
        },
        function(serverResponse) {
            var vmid;
            var cpu;
            var mem;
            var averagecpu;
            var averagemem;

            for (var i = 0; i < serverResponse.verbose.length; i += 1) {
                console.log(serverResponse);
                console.log(serverResponse.verbose.length);
                vmid = serverResponse.verbose[i].vmId;
                cpu = serverResponse.verbose[i].cpu;
                mem = serverResponse.verbose[i].mem;

                $('#adminWebHostingVMTable').dataTable().fnAddData([
                vmid, cpu, mem]);

                //  console.log(tnc );
                //  console.log(inputfile );
                //  console.log(command );
                //  console.log(state );
                //  console.log(result );
            }
            averagecpu = serverResponse.average.cpu;
            averagemem = serverResponse.average.mem;
            $('#adminWebHostingVMTable').dataTable().fnAddData(['Average', averagecpu, averagemem]);
        },
        'json');

        $('#adminWebHostingVMTableDialog').dialog('open');
    });

    adminWebHostingServiceInfoTable();
    //} );
};
